﻿using System;

namespace KuiHuaBaoDian.Services.Net {

    /// <summary>
    /// 网络连接句柄
    /// </summary>
    public readonly struct NetConnectionHandle {

        /// <summary>
        /// 空值
        /// </summary>
        public static readonly NetConnectionHandle None = new() { GUID = Guid.Empty };


        /// <summary>
        /// 是否处于连接状态
        /// </summary>
        public bool IsConnected => NetService.Instance.IsConnected(this);

        private readonly Guid m_GUID;
        private readonly int m_HashCode;

        /// <summary>
        /// GUID
        /// </summary>
        public Guid GUID {
            get => m_GUID;
            init {
                m_GUID = value;
                m_HashCode = m_GUID.GetHashCode();
            }
        }

        public override readonly string ToString() => $"[{nameof(NetConnectionHandle)}({m_GUID})]";

        public override readonly int GetHashCode() => m_HashCode;

        public override bool Equals(object obj) {
            if (obj is NetConnectionHandle other) {
                return other.m_GUID == m_GUID;
            }
            return false;
        }

        public static bool operator ==(NetConnectionHandle left, NetConnectionHandle right) => left.Equals(right);
        public static bool operator !=(NetConnectionHandle left, NetConnectionHandle right) => !left.Equals(right);
    }
}
